package com.zqxq.lottery.service.user.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.entity.po.user.UserCashPO;
import com.zqxq.lottery.entity.po.user.UserFundsPO;
import com.zqxq.lottery.entity.po.user.UserInfoPO;
import com.zqxq.lottery.entity.po.user.UserSignPO;
import com.zqxq.lottery.mapper.sys.SysConfigMapper;
import com.zqxq.lottery.mapper.user.UserCashMapper;
import com.zqxq.lottery.mapper.user.UserFundsMapper;
import com.zqxq.lottery.mapper.user.UserInfoMapper;
import com.zqxq.lottery.mapper.user.UserSignMapper;
import com.zqxq.lottery.service.user.IUserCashService;
import com.zqxq.lottery.service.user.IUserSignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;

@Service
@Slf4j
public class UserSignServiceImpl extends ServiceImpl<UserSignMapper, UserSignPO> implements IUserSignService {
    @Autowired
    private UserSignMapper userSignMapper;
    @Autowired
    private UserFundsMapper userFundsMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private YmlConfig ymlConfig;
    @Autowired
    private UserCashMapper userCashMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public Result signReward(Long userId) throws Exception {
        int count = userSignMapper.selectToDayCount(userId);
        if(count>0){
            return new Result(CodeEnum.FAIL_100002.getCode(),"您今天已经签到过了，一天只能签到一次！");
        }else{
            UserSignPO userSignPO = new UserSignPO();
            userSignPO.setUser_id(userId);
            userSignPO.setSign_time(new Date());
            userSignMapper.insert(userSignPO);
            UserFundsPO userFundsPO = userFundsMapper.selectById(userId);
            BigDecimal lotBean = userFundsPO.getLot_bean();
            SysConfigPO sysConfigPO = sysConfigMapper.getByKey("SIGN_REWARD");
            if(sysConfigPO==null){
                return new Result(CodeEnum.FAIL_100002.getCode(),"缺少配置参数");
            }
            userFundsPO.setLot_bean(lotBean.add(new BigDecimal(sysConfigPO.getCval())));
            UserInfoPO userInfo = userInfoMapper.selectById(userId);
            UserCashPO userCashPO = new UserCashPO();
            userCashPO.setId(ymlConfig.getPkId());
            userCashPO.setUser_id(userInfo.getId());
            userCashPO.setReal_name(userInfo.getReal_name());
            userCashPO.setMobile(userInfo.getMobile());
            userCashPO.setPay_way(7);
            userCashPO.setPay_account(null);
            userCashPO.setType(7);
            userCashPO.setStatus(4);
            userCashPO.setInfo("每日签到奖励");
            userCashPO.setPay_amount(new BigDecimal(sysConfigPO.getCval()));
            userCashPO.setRemainder(userFundsPO.getLot_bean());
            userCashMapper.insert(userCashPO);
            userFundsMapper.updateById(userFundsPO);
            userInfo.setLogin_time(new Date());
            userInfoMapper.updateById(userInfo);

        }
        return new Result(CodeEnum.SUCCESS,"签到成功！");
    }
}
